package com.mark.util;

import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/*
 * Created by Mark on 2018/4/12.
 * 图片处理工具类
 */
@Slf4j
public class ImageUtil{

    private static String basePath = Thread.currentThread().getContextClassLoader().getResource("").getPath();//获取当前线程的绝对值路径
    private static final SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
    private static final Random r = new Random();

    //获取新生的文件名
    public static String getNewFileName(String fileName){
        String fileExtensionName = fileName.substring(fileName.lastIndexOf(".")+1);//获取扩展名
        int randomNum = r.nextInt(89999) + 10000;// r.nextInt(89999)：0<x<89999
        String nowTimeStr = sDateFormat.format(new Date());
        return nowTimeStr + "-"+randomNum+"."+fileExtensionName;
    };

    //获取上传图片的绝对路径
    public static String getAbsolutePath(String targetAddress){
        String absolutePath = PathUtil.getImageBasePath()+targetAddress;
        return absolutePath;
    }

    //加上水印
    public static File generateThumbnail(File file,String targetAddress){
        String fileNewName = file.getName();
        String absolutePath = getAbsolutePath(targetAddress);
        makeDirPath(absolutePath);
        try {
            Thumbnails.
                    of(file)
                    .size(337,640)
                    //.watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File(basePath + "/image/watermark.jpg")),1.0f)
                    .outputQuality(0.8f)
                    .toFile(absolutePath+fileNewName);
        } catch (IOException e) {
            log.error("生成水印图片异常：{}",e.getMessage());
        }
        log.info("文件通过本地方法保存");
        return new File(absolutePath+fileNewName);
    }

    //加上水印
    public static void generateThumbnailUpload(File file){
        try {
            Thumbnails
                    .of(file) //将要添加水印的图片
                    .size(337,640) //加上水印后的尺寸大小
                   // .watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File(basePath + "/image/watermark.jpg")),1.0F) //水印的生成位置，水印图片
                    .outputQuality(0.8f) //生成的质量
                    .toFile(file.getAbsolutePath());//生成图片的路径

        } catch (IOException e) {
            log.error("生成水印图片异常：{}",e.getMessage());
        }
    }

    private static void makeDirPath(String absolutePath) {
        try{
            File dirPath = new File(absolutePath);
            if (!dirPath.exists()) {//如果不存在
                dirPath.setWritable(true,false);//赋予可写权限
                dirPath.mkdirs();
            }
            if (!new File(absolutePath).exists()) {
                log.error("创建文件：{} 失败",absolutePath);
            }
        }catch (Exception e){
            log.error("生成目标文路径:"+absolutePath+"异常:{}",e.getMessage());
        }

    }

    public static void main(String[] args){
        String targetAddress = PathUtil.getProductImagePath(1L);
        File file = new File("E:\\workspace_MARK\\image\\1.jpg");
        ImageUtil.generateThumbnail(file,targetAddress);
    }
}